﻿Public Class CustomersForm

    Private Sub CustomerBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CustomerBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.CustomerBindingSource.EndEdit()
        Me.ContactBindingSource.EndEdit()
        Me.IndividualBindingSource.EndEdit()
        Me.TableAdapterManager.CustomerTableAdapter.Update(Me.AdventureWorksDataSet.Customer)
        Me.TableAdapterManager.ContactTableAdapter.Update(Me.AdventureWorksDataSet.Contact)
        Me.TableAdapterManager.IndividualTableAdapter.Update(Me.AdventureWorksDataSet.Individual)
        'Me.TableAdapterManager.UpdateAll(Me.AdventureWorksDataSet)

    End Sub

    Private Sub CustomersForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'AdventureWorksDataSet.SalesOrderHeader' table. You can move, or remove it, as needed.
        Me.SalesOrderHeaderTableAdapter.Fill(Me.AdventureWorksDataSet.SalesOrderHeader)
        Me.TableAdapterManager.SalesTerritoryTableAdapter.Fill(Me.AdventureWorksDataSet.SalesTerritory)
        Me.TableAdapterManager.CustomerTableAdapter.Fill(Me.AdventureWorksDataSet.Customer)
        Me.TableAdapterManager.ContactTableAdapter.Fill(Me.AdventureWorksDataSet.Contact)
        Me.TableAdapterManager.IndividualTableAdapter.Fill(Me.AdventureWorksDataSet.Individual)
        Me.ControlValidator1.AddRules(CustomerValidationRules.Instance)
    End Sub

    Private Const cCustomerType As String = "I"
    Private Sub BindingNavigatorAddNewItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorAddNewItem.Click
        Dim drv As DataRowView

        drv = trycast(Me.CustomerBindingSource.AddNew, DataRowView)
        Dim customer = TryCast(drv.Row, AdventureWorksDataSet.CustomerRow)
        customer.rowguid = Guid.NewGuid
        customer.CustomerType = "I"
        customer.ModifiedDate = Now
        Me.CustomerBindingSource.EndEdit()


        drv = TryCast(Me.ContactBindingSource.AddNew, DataRowView)
        Dim contact = TryCast(drv.Row, AdventureWorksDataSet.ContactRow)
        contact.NameStyle = True
        contact.FirstName = "<first name>"
        contact.LastName = "<last name>"
        contact.EmailPromotion = 0
        contact.PasswordHash = ""
        contact.PasswordSalt = ""
        contact.ModifiedDate = Now
        contact.rowguid = Guid.NewGuid
        Me.ContactBindingSource.EndEdit()


        drv = TryCast(Me.IndividualBindingSource.AddNew, DataRowView)
        If drv Is Nothing Then Return
        Dim individual As AdventureWorksDataSet.IndividualRow = TryCast(drv.Row, AdventureWorksDataSet.IndividualRow)
        individual.CustomerRow = customer
        individual.ContactRow = contact
        individual.ModifiedDate = Now
        Me.IndividualBindingSource.EndEdit()

    End Sub


End Class
